package com.sfh.cpc.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.RspBase;
import com.sfh.cpc.com.req.ReqMassAdd;
import com.sfh.cpc.com.ui.SelectTakeImageActivity;
import com.sfh.cpc.com.widget.WidgetBaseToolbar;
import com.sfh.cpc.mass.model.MassAddModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;

import java.util.List;

/**
 * 功能描述: 添加新的社区
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/27
 */
public class MassAddActivity extends SelectTakeImageActivity<MassAddModel> {

    public static void startUI(Context context){

        Intent intent = new Intent(context,MassAddActivity.class);
        context.startActivity(intent);
    }
    WidgetBaseToolbar toolbar;
    ImageView ivHeader;
    EditText etTitle;
    EditText etDesc;
    RadioButton rbYES,rbNO;
    ReqMassAdd reqMassAdd = new ReqMassAdd();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_add_activity);
        this. toolbar = findViewById(R.id.toolbar);
        this.ivHeader = findViewById(R.id.ivHeader);
        this.etTitle = findViewById(R.id.etTitle);
        this.etDesc = findViewById(R.id.etDesc);
        this.rbYES = findViewById(R.id.rbYES);
        this.rbNO = findViewById(R.id.rbNO);
        this. toolbar.setTitle("添加社团");
        rbYES.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                findViewById(R.id.ly_vip).setVisibility(b ? View.VISIBLE : View.GONE);
            }
        });
    }

    public void onViewCilcked(View view) {
        if (view == ivHeader) {
            this.hasPermissions();
        } else {
            this.reqMassAdd.setVip(this.rbYES.isChecked() ? "1" : "0");
            this.reqMassAdd.setTitle(this.etTitle.getText().toString());
            this.reqMassAdd.setContent(this.etDesc.getText().toString());
            EditText et_wealth = findViewById(R.id.et_wealth);
            if (!TextUtils.isEmpty(et_wealth.getText().toString())) {
                reqMassAdd.setWealth(Long.valueOf(et_wealth.getText().toString()));
            }
            this.getViewModel().saveMass(this.reqMassAdd);
        }
    }

    @Override
    public void onSelectImageOK(List<String> select) {
        this.getViewModel().updateFile(select.get(0));
    }

    @LiveDataMatch
    public void onSaveFile(RspBase<String> data) {
        if (data.success()) {
            this.reqMassAdd.setUrl(data.getData());
            new ImageLoaderEngine(this, BaseAppContent.getImageUrl(data.getData())).resizeDimen(R.dimen.px_100, R.dimen.px_100).centerCrop().into(ivHeader);
        } else {
            this.showToast(data.getMsg());
        }
    }
    @LiveDataMatch
    public void onSaveMassSuccess(){
        showToast("创建成功");
        finish();
    }
}
